package com.mtzqc.common.ui.activity

import android.view.LayoutInflater
import android.view.View
import androidx.viewbinding.ViewBinding
import com.mtzqc.common.databinding.CommonTitleBarBinding
import com.mtzqc.common.kt.filterListener

abstract class TitleBarBindingActivity<VB : ViewBinding>(
    inflate: (LayoutInflater) -> VB
) : BarBindingActivity<VB, CommonTitleBarBinding>(inflate, CommonTitleBarBinding::bind) {

    open fun titleTxt(): CharSequence? {
        return null
    }

    override fun getTitleBar(bar: CommonTitleBarBinding): View? {
        return  bar.titleBar
    }
    override fun initBar(bar: CommonTitleBarBinding) {
        initTitleBarTitle(bar)
        initTitleBarLeft(bar)
    }

    open fun initTitleBarTitle(bar: CommonTitleBarBinding) {
        titleTxt()?.let {
            bar.titleBar.setTitle(it)
        }
    }

    open fun leftIcon(): Int {
        return if (isNightMode()) com.xuexiang.xui.R.drawable.xui_ic_action_navigation_arrow_back else com.xuexiang.xui.R.drawable.xui_ic_navigation_back_white
    }

    open fun initTitleBarLeft(bar: CommonTitleBarBinding) {
        var leftIcon = leftIcon()
        if (0 != leftIcon) {
            bar.titleBar.setLeftImageResource(leftIcon)
                .setLeftClickListener(filterListener(block = {
                    onTitleBarLeftClick(it)
                }, filter = {
                    !isFinishing && !isDestroyed
                }))
        }
    }


    open fun onTitleBarLeftClick(view: View) {
        finish()
    }

}